package com.cq.hd.order.validator;

import com.cq.hd.common.enums.biz.ActiveEnrollInfoMustStateEnum;
import com.cq.hd.common.enums.biz.ActiveEnrollInfoTypeEnum;
import com.cq.hd.common.response.Throw;
import com.cq.hd.order.api.dto.ActiveEnrollInfoSonDto;
import com.cq.hd.order.api.dto.CreateOrderDto;
import com.cq.hd.product.api.vo.ActiveVo;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 订单活动信息校验类
 *
 * @author much
 * @since 2024/05/22 21:43
 */
public class ActiveOrderEnrollInfoValidator {

    /**
     * 校验报名信息
     *
     * @param createOrderDto
     * @param activeVo
     */
    public static void validate(CreateOrderDto createOrderDto, ActiveVo activeVo) {
        // 报名信息
        List<ActiveEnrollInfoSonDto> getActiveEnrollInfos = createOrderDto.getActiveEnrollInfos();
        // 活动报名信息列表(配置)
        List<com.cq.hd.product.api.dto.ActiveEnrollInfoSonDto> activeEnrollInfos = activeVo.getActiveEnrollInfos();
        if (!CollectionUtils.isEmpty(activeEnrollInfos)) {
            // 判断报名信息是否正确
            if (CollectionUtils.isEmpty(getActiveEnrollInfos)) {
                Throw.isBusinessException("请填写报名信息");
            }

            Map<Long, ActiveEnrollInfoSonDto> activeEnrollInfoSonDtoMap = getActiveEnrollInfos.stream()
                    .collect(Collectors.toMap(com.cq.hd.order.api.dto.ActiveEnrollInfoSonDto::getId, Function.identity(), (k1, k2) -> k2));
            for (com.cq.hd.product.api.dto.ActiveEnrollInfoSonDto activeEnrollInfoSonDto : activeEnrollInfos) {
                Long id = activeEnrollInfoSonDto.getId();
                com.cq.hd.order.api.dto.ActiveEnrollInfoSonDto activeEnrollInfo = activeEnrollInfoSonDtoMap.get(id);
                if (activeEnrollInfo == null) {
                    Throw.isBusinessException("请填写报名信息");
                }

                String title = activeEnrollInfo.getTitle();
                // 是否必填（1-否，2-是）
                Integer mustState = activeEnrollInfo.getMustState();
                // 类型(1-填写项，2-单选项，3-多选项)
                Integer type = activeEnrollInfo.getType();
                // 内容（类型=填写项，用户填写）
                String content = activeEnrollInfo.getContent();
                // 内容（类型=单选项，不能为空，用户勾选）
                List<com.cq.hd.order.api.dto.ActiveEnrollInfoOptionDto> activeEnrollInfoOptions = activeEnrollInfo.getActiveEnrollInfoOptions();

                if (type == null) {
                    Throw.isBusinessException("报名信息填写错误");
                }

                // 填写的类型和数据库的类型不匹配
                if (!activeEnrollInfoSonDto.getType().equals(type)) {
                    Throw.isBusinessException("报名信息错误");
                }

                if (ActiveEnrollInfoTypeEnum.WRITE.getValue().equals(type)) {
                    if (ActiveEnrollInfoMustStateEnum.YES.getValue().equals(mustState) && StringUtils.isEmpty(content)) {
                        Throw.isBusinessException("请填写报名信息的" + title);
                    }
                } else if (ActiveEnrollInfoTypeEnum.SINGLE.getValue().equals(type)) {
                    if (CollectionUtils.isEmpty(activeEnrollInfoOptions)) {
                        Throw.isBusinessException("报名信息填写错误");
                    }

                    List<com.cq.hd.order.api.dto.ActiveEnrollInfoOptionDto> checkedOptions = new ArrayList<>();
                    for (com.cq.hd.order.api.dto.ActiveEnrollInfoOptionDto activeEnrollInfoOption : activeEnrollInfoOptions) {
                        // 勾选状态：1-未勾选，2-已勾选
                        Integer checkState = activeEnrollInfoOption.getCheckState();
                        if (checkState != null && checkState == 2) {
                            checkedOptions.add(activeEnrollInfoOption);
                        }
                    }

                    if (ActiveEnrollInfoMustStateEnum.YES.getValue().equals(mustState) && CollectionUtils.isEmpty(checkedOptions)) {
                        Throw.isBusinessException("请勾选报名信息的" + title);
                    }

                    if (checkedOptions.size() > 1) {
                        Throw.isBusinessException("报名信息" + title + "只支持单选");
                    }
                } else if (ActiveEnrollInfoTypeEnum.MULTIPLE.getValue().equals(type)) {
                    if (CollectionUtils.isEmpty(activeEnrollInfoOptions)) {
                        Throw.isBusinessException("报名信息填写错误");
                    }

                    List<com.cq.hd.order.api.dto.ActiveEnrollInfoOptionDto> checkedOptions = new ArrayList<>();
                    for (com.cq.hd.order.api.dto.ActiveEnrollInfoOptionDto activeEnrollInfoOption : activeEnrollInfoOptions) {
                        // 勾选状态：1-未勾选，2-已勾选
                        Integer checkState = activeEnrollInfoOption.getCheckState();
                        if (checkState != null && checkState == 2) {
                            checkedOptions.add(activeEnrollInfoOption);
                        }
                    }

                    if (ActiveEnrollInfoMustStateEnum.YES.getValue().equals(mustState) && CollectionUtils.isEmpty(checkedOptions)) {
                        Throw.isBusinessException("请勾选报名信息的" + title);
                    }
                } else {
                    Throw.isBusinessException("报名信息填写错误");
                }
            }
        }
    }

}
